<%@ page import="java.sql.*" %> 
<%@ page import="java.io.*" %> 
<%@ page import="java.util.Date" %> 
<%@ page import="java.text.DateFormat" %> 
<%@ page import="java.text.SimpleDateFormat" %> 

<%

	Boolean chk_flag = true;
	//to get the content type information from JSP Request Header
	String contentType = request.getContentType();
	//here we are checking the content type is not equal to Null and
	//as well as the passed data from mulitpart/form-data is greater than or
	//equal to 0
	
	int _startPos = 0;
	int _endPos = 0;
	int _formDataLength = 0;
	String _saveFile = "";	
	
	if ((contentType != null) && (contentType.indexOf("multipart/form-data") >= 0)) 
	{
		DataInputStream in = new DataInputStream(request.getInputStream());
		//we are taking the length of Content type data
		int formDataLength = request.getContentLength();
		byte dataBytes[] = new byte[formDataLength];
		int byteRead = 0;
		int totalBytesRead = 0;
		//this loop converting the uploaded file into byte code
		while (totalBytesRead < formDataLength) 
		{
			byteRead = in.read(dataBytes, totalBytesRead, formDataLength);
			totalBytesRead += byteRead;
		}
		String file = new String(dataBytes);
		//for saving the file name
		String saveFile = file.substring(file.indexOf("filename=\"") + 10);
		saveFile = saveFile.substring(0, saveFile.indexOf("\n"));
		saveFile = saveFile.substring(saveFile.lastIndexOf("\\")+ 1,saveFile.indexOf("\""));
		int lastIndex = contentType.lastIndexOf("=");
		String boundary = contentType.substring(lastIndex + 1,contentType.length());
		int pos;
		//extracting the index of file 
		pos = file.indexOf("filename=\"");
		pos = file.indexOf("\n", pos) + 1;
		pos = file.indexOf("\n", pos) + 1;
		pos = file.indexOf("\n", pos) + 1;
		int boundaryLocation = file.indexOf(boundary, pos) - 4;
		int startPos = ((file.substring(0, pos)).getBytes()).length;
		int endPos = ((file.substring(0, boundaryLocation)).getBytes()).length;
		int maxFileSize = (1024 * 1024 * 3);
		
		_startPos = startPos;
		_endPos = endPos;
		_formDataLength = formDataLength;
		_saveFile = saveFile;
	}
	
	try {
	
		String connectionURL = "jdbc:mysql://localhost:3306/unilearn"; 
		
		int c_row_count = 0;
		Connection connection = null;
		Statement stmt = null;
		ResultSet rst=null;

		Class.forName("com.mysql.jdbc.Driver").newInstance(); 

		connection = DriverManager.getConnection(connectionURL, "root", "");
		stmt = connection.createStatement();
		
		if(!connection.isClosed())
		{
			
			//get session from assignment create page
			String assign_name = (String)session.getAttribute("AssCreate_AssignName");
			String e_dd = (String)session.getAttribute("AssCreate_AssignEdd");
			String e_mm = (String)session.getAttribute("AssCreate_AssignEmm");
			String e_yy = (String)session.getAttribute("AssCreate_AssignEyy");
			String assign_status = (String)session.getAttribute("AssCreate_AssignStatus");
			String assign_type = (String)session.getAttribute("AssCreate_AssignType");
			String desc = (String)session.getAttribute("AssCreate_Desc");
			String class_group = (String)session.getAttribute("AssCreate_ClassGroup");
			String upload_file = (String)session.getAttribute("AssCreate_FileName");
			String late_penalty = (String)session.getAttribute("AssCreate_LatePenalty");
			String subject_id = (String)session.getAttribute("AssCreate_SubjectID");
			
			// commin session
			String user_id = (String)session.getAttribute("UserID");
			
			DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			Date _now = new Date();
			
			String full_e_date = "";
			if (e_dd != "" && e_mm != "" && e_yy != "")
			{
				full_e_date = e_yy + "-" + e_mm + "-" + e_dd + " 00:00:00";
			}
				
			if((assign_name != null && assign_name != ""))
			{ 
				c_row_count = stmt.executeUpdate("INSERT INTO assignment (assignment_name, assignment_status , assignment_type, class_group_id, subject_id, start_date, end_date, spec_file_path, description, late_penalty_flag, create_user, create_date, modify_date )" 
				+ "VALUES ('"+assign_name+"', '"+assign_status+"', '"+assign_type+"', 0, '"+subject_id+"', '"+dateFormat.format(_now)+"', '"+full_e_date+"', '"+upload_file+"', '"+desc+"', '"+late_penalty+"', '"+user_id+"', '"+ dateFormat.format(_now) +"', '"+dateFormat.format(_now)+"')");
				
				
				if (c_row_count > 0)
				{
					
					byte tmpfile[] = new byte[_formDataLength];
					
					if(_saveFile.length() > 0){
						//FileOutputStream fileOut = new FileOutputStream("//usr//local//apache-tomcat-7.0.19//webapps//ROOT//2Sep//assignment_refer//" + _saveFile);
						FileOutputStream fileOut = new FileOutputStream(".\\assignment_refer\\" + _saveFile);
						fileOut.write(tmpfile, _startPos, (_endPos - _startPos));
						fileOut.flush();
						fileOut.close();
					}
					
					rst = stmt.executeQuery("SELECT MAX(assignment_id) as last_id FROM assignment");
					
					int last_assignment_id = 0;
					while(rst.next())
					{
						last_assignment_id = Integer.parseInt(rst.getString("last_id"));
					}
					
					out.println( "<script>alert('assignment create successful');window.location='markingSchema.jsp?AssignmentID="+last_assignment_id+"'</script>" );
				}else
				{
					out.println( "<script>alert('assignment create failed')</script>" );
				}
			}
		}
	}catch(Exception ex){
	out.println(ex);
}							
%>

